#pragma once
#pragma once

#include <assert.h>
namespace hsl
{
	template <class T>
	class vector
	{

	public:
		typedef T* iterator;
		iterator begin()
		{
			return _start;
		}

		iterator end()
		{
			return _finish;
		}


		vector()
		{}


		vector(const vector<T>& v)
		{
			reserve(v.capacity());
			for (auto& e : v)
			{
				push_back(e);
			}
		}


		template <class InputIterator>
		vector(InputIterator first, InputIterator last)
		{
			while (first != last)
			{
				push_back(*first);
				first++;
			}
		}

		vector(size_t n,const T& val=T())
		{
			for (size_t i = 0; i < n; i++)
			{
				push_back(val);
			}
		}

		vector(int n, const T& val = T())
		{
			for (int i = 0; i < n; i++)
			{
				push_back(val);
			}
		}

		void swap(vector<T>& v)
		{
			std::swap(_start, v._start);
			std::swap(_finish,v._finish );
			std::swap(_endofstorage, v._endofstorage);
		}

		vector<T>& operator =(vector<T>& v)
		{
			swap(v);
			return *this;
		}

		~vector()
		{
			delete[] _start;
			_start = _finish = _endofstorage = nullptr;
		}
		void push_back(const T& x)
		{
			if (_finish == _endofstorage)
			{
				size_t cp;
				reserve(cp = capacity() == 0 ? 4 : capacity() * 2);
			}
			*_finish = x;
			++_finish;
		}

		size_t capacity() const
		{
			return _endofstorage - _start;
		}

		size_t size() const

		{
			return _finish - _start;
		}


		void reserve(size_t n)
		{
			if (n > capacity())
			{
				T* tmp = new T[n];
				size_t sz = size();
				if (_start)
				{
				//	memcpy(tmp, _start, sizeof(T) * n);
					for (size_t i = 0; i < size(); i++)
					{
						tmp[i] = _start[i];
					}
					delete[] _start;
				}
				_start = tmp;
				_finish = _start+sz;
				_endofstorage = _start + n;
			}
		}
		
		void resize(size_t n,T val = T())
		{
			if (n <= size())
			{
				_finish = _start + n;
			}
			else
			{
				reserve(n);
				/*int begin = size();
				while (begin < n)
				{
					push_back(val);
					begin++;
				}*/
				while (_finish < _start + n)
				{
					*(_finish) = val;
					++_finish;
				}
			}
		}

		void insert(iterator pos,const T& x)
		{
			assert(pos >= _start);
			assert(pos <= _finish);
			if (_finish == _endofstorage)
			{
				size_t len = pos - _start;
				reserve(capacity() == 0 ? 4 : capacity() * 2);
				pos = _start + len;
			}
			iterator end = _finish - 1;
			while (end >= pos)
			{
				*(end + 1) = *end;
				end--;
			}
			*pos = x;
			++_finish;
		}

		iterator erase(iterator pos)
		{
			assert(pos >= _start);
			assert(pos < _finish);
			auto it = pos;
			while (it < _finish)
			{
				*it = *(it + 1);
				it++;
			}
			--_finish;
			return pos;
		}


		T& operator[](size_t pos)
		{
			assert(pos < size());
			return _start[pos];
		}

	private:
		iterator _start=nullptr;
		iterator _finish=nullptr;
		iterator _endofstorage=nullptr;
	};

	void test_vector1()
	{
		vector<int> v1;
		v1.push_back(1);
		v1.push_back(2);
		v1.push_back(4);
		v1.push_back(4);
		v1.push_back(5);
		v1.push_back(6);


		for (auto e : v1)
		{
			cout << e << " ";
		}
		cout << endl;
		auto it = v1.begin();

		while (it != v1.end())
		{
			if (*it % 2 == 0)
			{
				it=v1.erase(it);
			}
			else
			{
				it++;
			}
		}

		for (auto e : v1)
		{
			cout << e<<" ";
		}
		cout << endl;
	}

	void test_vector2()
	{
		vector<string> v1;
		v1.push_back("xxxxxxxxxxxxxxxxxxxxxxxxxxxx");
		v1.push_back("xxxxxxxxxxxxxxxxxxxxxxxxxxxx");
		v1.push_back("xxxxxxxxxxxxxxxxxxxxxxxxxxxx");
		v1.push_back("xxxxxxxxxxxxxxxxxxxxxxxxxxxx");
		v1.push_back("xxxxxxxxxxxxxxxxxxxxxxxxxxxx");

		for (auto e : v1)
		{
			cout << e << " ";
		}
		cout << endl;
	}
	void test_vector3()
	{

		vector<int> v1(10,1);
		vector<int> v2;
		v2 = v1;
			

		for (auto e : v2)
		{
			cout << e << " ";
		}
		cout << endl;
	}
}
